<?php
/**
 * SessionMysql.class.php   session mysql 数据库存储类
 *
 * @author                  mashanling(msl-138@163.com)
 * @date                    2012-09-18 13:45:05
 * @last modify             2012-09-19 08:43:22 by mashanling
 */
class SessionMysql {
    private $lifetime = 1800;
    private $model;
    private $table;

    /**
     * 构造函数
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-18 13:54:19
     * @last modify  2012-09-18 13:54:19 by mashanling
     *
     * @return void 无返回值
     */
    function __construct() {
        $this->model = D('Session');
        $this->lifetime = sys_config('sys_session_gc_maxlifetime');
        session_set_save_handler(array(&$this, 'open'), array(&$this, 'close'), array(&$this, 'read'), array(&$this, 'write'), array(&$this, 'destroy'), array(&$this, 'gc'));
    }

    /**
     * session_set_save_handler  close方法
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-18 14:40:44
     * @last modify  2012-09-18 14:40:44 by mashanling
     *
     * @return bool
     */
    function close() {
        return true;
        //return $this->gc($this->lifetime);
    }

    /**
     * 删除指定的session_id
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-18 14:40:44
     * @last modify  2012-09-19 08:43:47 by mashanling
     *
     * @param string $session_id session_id
     *
     * @return bool
     */
    function destroy($session_id) {
        return $this->model->where("session_id='{$session_id}'")->delete();
    }

    /**
     * 删除过期的 session
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-18 14:41:50
     * @last modify  2012-09-19 08:43:56 by mashanling
     *
     * @param $maxlifetime 存活期时间
     *
     * @return bool
     */
    function gc($maxlifetime) {
        return $this->model->where('last_time<' . (APP_NOW_TIME - $maxlifetime))->delete();
    }

    /**
     * session_set_save_handler  open方法
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-18 14:04:06
     * @last modify  2012-09-18 14:04:06 by mashanling
     *
     * @param $save_path
     * @param $session_name
     *
     * @return true
     */
    function open($save_path, $session_name) {
        return true;
    }

    /**
     * 读取session_id
     * session_set_save_handler  read方法
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-18 16:53:41
     * @last modify  2012-09-19 08:43:14 by mashanling
     *
     * @param string $session_id session_id
     *
     * @return string session值
     */
    function read($session_id) {
        $data = $this->model->where("session_id='{$session_id}'")->getField('data');

        return $data === false ? '' : $data;
    }

    /**
     * 写入session_id 的值
     *
     * @author       mashanling(msl-138@163.com)
     * @date         2012-09-18 16:38:23
     * @last modify  2012-09-19 08:43:07 by mashanling
     *
     * @param string $session_id session_id
     * @param string $data       session数据
     *
     * @return bool
     */
    function write($session_id, $data) {
        $data = array('session_id' => $session_id, 'data' => $data);
        $this->model->autoOperation($data, Model::MODEL_BOTH);

        return $this->model->add($data, array(), 1);
    }
}